package com.gin.ehdownloader.utils;

import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字符串工具类
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/3/31 16:52
 */
public class StrUtils {
    /**
     * 解析范围字符串
     * @param range 范围
     * @return 序号
     */
    public static List<Integer> parseRange(String range) {
        final ArrayList<Integer> list = new ArrayList<>();
        if (ObjectUtils.isEmpty(range)) {
            return list;
        }
        Arrays.stream(range.split(","))
                .map(s -> s.split("-"))
                .forEach(r -> {
                    if (r.length == 1) {
                        list.add(Integer.parseInt(r[0]) - 1);
                    }
                    if (r.length == 2 && !"".equals(r[0]) && !"".equals(r[1])) {
                        int a0 = Integer.parseInt(r[0]);
                        int a1 = Integer.parseInt(r[1]);
                        final int min = Math.min(a0, a1);
                        final int max = Math.max(a0, a1) + 1;
                        for (int i = min; i < max; i++) {
                            list.add(i - 1);
                        }
                    }
                });
        return list;
    }
}   
